/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.util;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

/**
 * class designed specific for shape file copy. It combines all files in one dir with same file name and different extension and copies them al.
 * @author Merijn Zengers, Gerben Feenstra
 */
public class ShapeFileCopier extends GenericFileCopier {

    @Override
    public File copyDirectory(File source, File destination) throws NotADirectoryException, NotAFileException, DirectoryCreationException, FileNotFoundException, IOException, NoSuchAlgorithmException {
        if (!source.isFile()) {
            for (File f : source.listFiles()) {
                if (f.isFile()) {
                    if (f.getName().toLowerCase().endsWith(".shp")) {
                        copyFile(f, destination);
                    }
                }
            }
        }
        return new File(destination.getPath());
    }

    @Override
    public File copyFile(File source, File destination) throws NotADirectoryException, NotAFileException, DirectoryCreationException, FileNotFoundException, IOException, NoSuchAlgorithmException {
        File returnFile = null;
        if (source.isFile()) {
            for (File f : source.getParentFile().listFiles()) {
                if (f.isFile()) {
                    String shpFile = source.getName().substring(0, source.getName().indexOf("."));
                    String bundleFiles = f.getName().substring(0, f.getName().indexOf("."));
                    if (!shpFile.equals("") && !bundleFiles.equals("")) {
                        if (shpFile.equals(bundleFiles)) {
                            File temp = super.copyFile(f, destination);
                            if (temp.getName().toLowerCase().endsWith(".shp")) {
                                returnFile = temp;
                            }
                        }
                    }
                }
            }
        }
        return returnFile;
    }

    @Override
    public void moveFile(File source, File destination, boolean overwrite) throws NotADirectoryException, NotAFileException, DirectoryCreationException, FileMoveException {
        if (source.isFile()) {
            for (File f : source.getParentFile().listFiles()) {
                if (f.isFile()) {
                    String shpFile = source.getName().substring(0, source.getName().indexOf("."));
                    String bundleFiles = f.getName().substring(0, f.getName().indexOf("."));
                    if (!shpFile.equals("") && !bundleFiles.equals("")) {
                        if (shpFile.equals(bundleFiles)) {
                            super.moveFile(f, destination, overwrite);
                        }
                    }
                }
            }
        }
    }
}
